<template>
  <div class="table-page">
    <GiTable
      row-key="id"
      title="分类管理"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #custom-left>
	        <a-input v-model="queryForm.name" placeholder="请输入分类名称" allow-clear @change="search">
	          <template #prefix><icon-search /></template>
	        </a-input>
	        <a-select
	          v-model="queryForm.status"
	          :options="category_search_status"
	          placeholder="请选择状态"
	          allow-clear
	          style="width: 150px"
	          @change="search"
	        />	        
        <a-button @click="reset">重置</a-button>
      </template>
      <template #custom-right>
        <a-button v-permission="['common:category:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <span>新增</span>
        </a-button>
        <a-tooltip content="导出">
          <a-button v-permission="['common:category:export']" class="gi_hover_btn-border" @click="onExport">
            <template #icon>
              <icon-download />
            </template>
          </a-button>
        </a-tooltip>
      </template>
      <template #type="{ record }">
        <span v-if="record.type === 1">产品分类</span>
      </template>
      <template #status="{ record }">
        <a-switch
          @change="onChangeStatus(record)"
          v-model=record.status
          :checked-value="true"
          :unchecked-value="false"
          checked-text="是"
          unchecked-text="否"
          type="round"
        />
      </template>

      <template #action="{ record }">
        <a-space>
          <a-link v-if="record.pid === 0" @click="onAddSub(record)">添加子目录</a-link>
          
          <a-link v-permission="['common:category:update']" @click="onUpdate(record)">修改</a-link>
          <a-link
            v-permission="['common:category:delete']"
            status="danger"
            :disabled="record.disabled"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>



    <CategoryAddModal ref="CategoryAddModalRef" @save-success="search" />
  </div>
</template>

<script setup lang="ts">
import CategoryAddModal from './CategoryAddModal.vue'
import { type CategoryResp, type CategoryQuery, deleteCategory, exportCategory, listCategory,treeCategory,updateCategoryStatus } from '@/apis'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import { useDict } from '@/hooks/app'

defineOptions({ name: 'Category' })

const queryForm = reactive<CategoryQuery>({
  name: undefined,
  status: "-1",
  type: "1",
  sort: ['createTime,desc']
})

const { category_search_status } = useDict('category_search_status')

const {
  tableData: dataList,
  loading,
  search,
  handleDelete
} = useTable((page) => treeCategory(queryForm), { immediate: true })

const columns: TableInstanceColumns[] = [
  
  { title: '名称', dataIndex: 'name', slotName: 'name' },
  { title: '类型', dataIndex: 'type', slotName: 'type' },
  { title: '排序', dataIndex: 'sort', slotName: 'sort' },
  { title: '状态', dataIndex: 'status', slotName: 'status' },
  // { title: '路径', dataIndex: 'path', slotName: 'path' },
  {
    title: '操作',
    slotName: 'action',
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
  }
]

// 重置
const reset = () => {
  queryForm.name = undefined
  queryForm.status = undefined
  search()
}

// 更改状态
const onChangeStatus = (item: CategoryResp) => {
  updateCategoryStatus(item.id)
}

// 删除
const onDelete = (item: CategoryResp) => {
  return handleDelete(() => deleteCategory(item.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportCategory(queryForm))
}

const CategoryAddModalRef = ref<InstanceType<typeof CategoryAddModal>>()
// 新增
const onAdd = () => {
  CategoryAddModalRef.value?.onAdd()
}

// 新增子目录
const onAddSub = (item: CategoryResp) => {

  CategoryAddModalRef.value?.onAddSub(item.id)
}

// 修改
const onUpdate = (item: CategoryResp) => {
  CategoryAddModalRef.value?.onUpdate(item.id)
}

</script>

<style lang="scss" scoped></style>
